package com.chen.Sign.controller;

import com.chen.Sign.common.Result;
import com.chen.Sign.mapper.Blog_imagesMapper;
import com.chen.Sign.mapper.UserMapper;
import com.chen.Sign.pojo.Blog_images;
import com.chen.Sign.service.impl.Blog_imagesServiceImpl;
import com.chen.Sign.utils.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

// 博客图片模块
@Slf4j
@RestController
@SuppressWarnings({"all"})
@RequestMapping("/blog")
public class Blog_imagesController {

    @Autowired
    Blog_imagesServiceImpl blog_imagesService;

    @Autowired
    Blog_imagesMapper blog_imagesMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    UserMapper userMapper;

    @PostMapping("/addblogimages")
    public Result addBlogImages(@RequestPart("imagesaddress") MultipartFile[] imagesaddress, HttpServletRequest request) throws IOException {
        Result result = new Result();
        Map<String, byte[]> map = new HashMap<>();
        Blog_images blog_images = new Blog_images();
        String localpath = "/usr/images";
        if (imagesaddress.length > 0) {
            String Filepaths = "";
            for (int i = 0; i < imagesaddress.length; i++) {
                String originalFilename = imagesaddress[i].getOriginalFilename();
                String realpath = FileUtils.upload(imagesaddress[i],localpath,originalFilename);
                Filepaths += realpath;
                Filepaths += ",";
            }
            blog_images.setImagesaddress(Filepaths);
            int b = blog_imagesMapper.insert(blog_images);
            if (b > 0) {
                // code 1 上传成功
                return new Result(1,"上传成功","");
            } else {
                // code 0 上传失败
                return new Result(0,"上传失败","");
            }
        } else {
            // 图片为空 code 3
            return new Result(3,"图片为空","");
        }
    }

    // 回显博客图片
    @GetMapping("/getbi/{id}")
    @ResponseBody
    public Result getImage(@PathVariable("id") Integer id, HttpServletResponse response) throws Exception {
        Blog_images blog_images = blog_imagesService.getById(id);
        String Filepath = blog_images.getImagesaddress();
        // 利用逗号将图片地址都分开
        String[] g = Filepath.split(",");
        // 循环地址拿到图片转换为二进制数据,然后利用逗号分隔
        String result = "";
        BASE64Encoder encoder = new sun.misc.BASE64Encoder();
        BufferedImage bi;
        for(int i = 0;i < g.length; i++){
            File f = new File(g[i]);
            bi = ImageIO.read(f);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(bi, "jpg", baos);  //经测试转换的图片是格式这里就什么格式，否则会失真
            byte[] bytes = baos.toByteArray();
            String trim = encoder.encodeBuffer(bytes).trim();
            result += trim;
            result += ",";
        }
        // 一起返回给前端
        return new Result(1,"回显博客图片成功",result);
    }
}
